package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.domain.entity.XcxUser;
import com.ruoyi.common.utils.CurrencyUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.DemandOrder;
import com.ruoyi.yipaiju.domain.DemandOrderFollower;
import com.ruoyi.yipaiju.service.IDemandOrderFollowerService;
import com.ruoyi.yipaiju.service.IDemandOrderService;
import com.ruoyi.yipaiju.service.IXcxUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 好房
 */
@RestController
@RequestMapping("/demand")
public class DemandController extends BaseController {

    @Autowired
    private IDemandOrderService demandOrderService;
    @Autowired
    private IDemandOrderFollowerService demandOrderFollowerService;
    @Autowired
    private IXcxUserService xcxUserService;

    /**
     * 生成好房订单
     * @param demandOrder
     * @return
     */
    @PostMapping("/orders")
    public ApiResult orders(@RequestBody DemandOrder demandOrder) {
        demandOrder.setNo(CurrencyUtil.create(10));
        demandOrder.setClient(SecurityUtils.getUserId());
        demandOrderService.insertDemandOrder(demandOrder);
        return ApiResult.success();
    }

    /**
     * 好房预约详情
     * @param id
     * @return
     */
    @GetMapping("/orders/{id}")
    public ApiResult ordersInfo(@PathVariable("id") Long id){
        DemandOrder demandOrder = demandOrderService.selectDemandOrderById(id);

        List<DemandOrderFollower> demandOrderFollowers = demandOrderFollowerService.selectDemandOrderFollowerList(Builder.of(DemandOrderFollower::new).with(DemandOrderFollower::setDemandOrderId, demandOrder.getId()).build());
        demandOrder.setDemandOrderFollowers(demandOrderFollowers);

        List<Long> ids = demandOrderFollowers.stream().map(item -> Long.valueOf(item.getMarketer())).distinct().collect(Collectors.toList());
        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        demandOrderFollowers.stream().forEach(item -> {
            XcxUser xcxUser = xcxUserMap.get(Long.valueOf(item.getMarketer()));
            if (xcxUser != null){
                item.setImg(xcxUser.getAvatarUrl());
                item.setMarkName(xcxUser.getNickname());
                item.setTelphone(xcxUser.getPhone());
            }
        });
        return ApiResult.success(demandOrder);
    }

    /**
     * 好房跟单
     * @return
     */
    @PostMapping("/orders/followers")
    public ApiResult ordersFollowers(@RequestBody DemandOrderFollower demandOrderFollower){
        if (StringUtils.isEmpty(demandOrderFollower.getMarketer())){
            demandOrderFollower.setMarketer(SecurityUtils.getUserId().toString());
        }
        DemandOrderFollower demandOrderFollower1 = demandOrderFollowerService.selectOneDemandOrderFollower(demandOrderFollower);
        //  TODO 只能一个人接单  这个判断有问题  再说吧

        if (demandOrderFollower1 == null){
            demandOrderFollower1 = Builder.of(DemandOrderFollower::new)
                    .with(DemandOrderFollower::setDemandOrderId, demandOrderFollower.getDemandOrderId())
//                    .with(ForeclosedHouseBookingorderFollower::setMarketer, SecurityUtils.getUserId().toString())
                    .with(DemandOrderFollower::setMarketer, demandOrderFollower.getMarketer())
                    .with(DemandOrderFollower::setStage, 1)
                    .build();
            demandOrderFollowerService.insertDemandOrderFollower(demandOrderFollower1);
            DemandOrder demandOrder = demandOrderService.selectDemandOrderById(demandOrderFollower.getDemandOrderId());
            demandOrder.setStage(2);
            demandOrderService.updateDemandOrder(demandOrder);
        }
        DemandOrder demandOrder = demandOrderService.selectDemandOrderById(demandOrderFollower1.getDemandOrderId());
        if (demandOrder != null) {
            demandOrderFollower1.setDemandOrder(demandOrder);
            return ApiResult.success(demandOrderFollower1);
        }
        return ApiResult.error();
    }

    /**
     * 设置备注和下一步状态更新
     * @param id
     * @param demandOrderFollower
     * @return
     */
    @PostMapping("/orders/followers/{id}")
    public ApiResult ordersFollowersInfo(@PathVariable("id") Long id, @RequestBody DemandOrderFollower demandOrderFollower){
        DemandOrderFollower demandOrderFollower1 = demandOrderFollowerService.selectDemandOrderFollowerById(id);
        if (demandOrderFollower1 != null){
            if (demandOrderFollower.getRemark() != null){
                demandOrderFollower1.setRemark(demandOrderFollower.getRemark());
            }
            if (demandOrderFollower.getStage() != null){
                demandOrderFollower1.setStage(demandOrderFollower.getStage());
                if (demandOrderFollower.getStage().intValue() == 2){
                    demandOrderFollower1.setGetDemandTime(new Date());
                }
                if (demandOrderFollower.getStage().intValue() == 3){
                    demandOrderFollower1.setOlInviteTime(new Date());
                }

                if (demandOrderFollower.getStage().intValue() == 4){
                    demandOrderFollower1.setInterviewTime(new Date());
                }
                if (demandOrderFollower.getStage().intValue() == 5){
                    demandOrderFollower1.setFinishTime(new Date());
                    DemandOrder demandOrder = demandOrderService.selectDemandOrderById(demandOrderFollower1.getDemandOrderId());
                    demandOrder.setStage(3);
                    demandOrderService.updateDemandOrder(demandOrder);
                }
            }

            DemandOrder demandOrder = demandOrderService.selectDemandOrderById(demandOrderFollower1.getDemandOrderId());
            demandOrderFollower1.setDemandOrder(demandOrder);
            demandOrderFollowerService.updateDemandOrderFollower(demandOrderFollower1);
            return ApiResult.success(demandOrderFollower1);
        }
        return ApiResult.error();
    }


}
